<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据库公共字段自动填充：五种方案的深度对比</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e0e0e0, transparent);
            margin: 3rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
            position: relative;
            overflow: hidden;
        }
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .comparison-card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            position: relative;
            overflow: hidden;
        }
        .comparison-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }
        .quote-box {
            border-left: 4px solid #667eea;
            padding-left: 1.5rem;
            margin: 2rem 0;
            font-style: italic;
            color: #4a5568;
        }
        .tech-badge {
            display: inline-block;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.25rem 1rem;
            border-radius: 20px;
            font-size: 0.875rem;
            margin: 0.25rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    数据库公共字段自动填充
                </h1>
                <p class="text-2xl md:text-3xl mb-8 opacity-90">
                    五种方案的深度对比与实战指南
                </p>
                <div class="flex flex-wrap justify-center gap-4 mb-12">
                    <span class="tech-badge"><i class="fas fa-database mr-2"></i>MyBatis-Plus</span>
                    <span class="tech-badge"><i class="fas fa-layer-group mr-2"></i>Hibernate</span>
                    <span class="tech-badge"><i class="fas fa-code-branch mr-2"></i>Spring AOP</span>
                    <span class="tech-badge"><i class="fas fa-cogs mr-2"></i>JPA</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <div class="prose prose-lg max-w-none">
                <p class="text-xl leading-relaxed text-gray-700">
                    <span class="drop-cap">你</span>真的会处理那些烦人的创建时间、更新人字段吗？别急着点头。我曾见过一个资深架构师在code review中怒斥初级开发："为什么每个dao都要手动set这些字段？这是多大的技术债！"而另一位技术总监则坚持："所有'魔法'自动填充都是定时炸弹，生产环境别给我玩花样！"
                </p>
                
                <div class="quote-box mt-8">
                    <p class="text-lg">
                        更有趣的是，各大厂的最佳实践完全相反：阿里推崇mybatis-plus自动填充，而字节则在代码规范中明确要求"所有字段必须显式赋值"。当你用Spring Data JPA的@CreateDate时，有没有想过在高并发场景会增加20%的数据库压力？
                    </p>
                </div>

                <div class="highlight-box">
                    <h3 class="text-2xl font-bold mb-3"><i class="fas fa-question-circle mr-2"></i>核心问题</h3>
                    <p class="text-lg">手动填充真的就低级吗？AOP切面就真的是万能解决方案？让我们深入探讨五种主流方案的优劣。</p>
                </div>
            </div>
        </div>

        <!-- 方案概览图 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 gradient-text text-center">五种方案技术架构对比</h2>
            <div class="mermaid">
                graph TB
                    A[公共字段填充需求] --> B[手动填充]
                    A --> C[MyBatis-Plus]
                    A --> D[Hibernate拦截器]
                    A --> E[Spring AOP]
                    A --> F[JPA监听器]
                    
                    B --> B1[Service层手动设置]
                    C --> C1[MetaObjectHandler]
                    D --> D1[Interceptor接口]
                    E --> E1[切面+切点表达式]
                    F --> F1[@EntityListeners]
                    
                    style A fill:#667eea,stroke:#fff,color:#fff
                    style B fill:#f093fb,stroke:#fff,color:#fff
                    style C fill:#4facfe,stroke:#fff,color:#fff
                    style D fill:#43e97b,stroke:#fff,color:#fff
                    style E fill:#fa709a,stroke:#fff,color:#fff
                    style F fill:#30cfd0,stroke:#fff,color:#fff
            </div>
        </div>

        <!-- 方案一：传统手动填充 -->
        <div class="comparison-card mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-hand-paper text-purple-600 mr-3"></i>
                方案一：传统手动填充法及其痛点
            </h2>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-700">Service层手动设置的传统做法</h3>
            <p class="text-gray-600 mb-4">
                最原始也是最直接的方法，就是在每个Service的save或update方法中手动设置这些公共字段：
            </p>
            
            <div class="code-block mb-6">
@Service
public class UserServiceImpl implements UserService {
    
    @Override
    public void saveUser(User user) {
        // 手动设置创建时间和创建人
        user.setCreateTime(new Date());
        user.setCreateBy(getCurrentUsername());
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        // 手动设置更新时间和更新人
        user.setUpdateTime(new Date());
        user.setUpdateBy(getCurrentUsername());
        userMapper.updateById(user);
    }

    private String getCurrentUsername() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }
}
            </div>

            <div class="grid md:grid-cols-2 gap-6 mt-8">
                <div class="bg-red-50 p-6 rounded-lg">
                    <h4 class="text-lg font-semibold text-red-700 mb-3">
                        <i class="fas fa-exclamation-triangle mr-2"></i>重复代码带来的维护噩梦
                    </h4>
                    <p class="text-gray-700">
                        假设你有50个实体类，每个都需要这四个公共字段，那就得在50个Service中写近乎相同的代码，这显然违背了DRY原则。
                    </p>
                </div>
                <div class="bg-yellow-50 p-6 rounded-lg">
                    <h4 class="text-lg font-semibold text-yellow-700 mb-3">
                        <i class="fas fa-bug mr-2"></i>容易遗漏导致的数据不一致
                    </h4>
                    <p class="text-gray-700">
                        新来的团队成员可能不了解这个"潜规则"，忘记设置这些字段，导致生产环境中出现大量的数据不一致问题。
                    </p>
                </div>
            </div>
        </div>

        <!-- 方案二：MyBatis-Plus -->
        <div class="comparison-card mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-magic text-blue-600 mr-3"></i>
                方案二：MyBatis-Plus自动填充神器
            </h2>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-700">MetaObjectHandler接口介绍</h3>
            <p class="text-gray-600 mb-4">
                MyBatis-Plus提供了一个MetaObjectHandler接口，只需实现这个接口，就可以自定义字段自动填充的逻辑：
            </p>
            
            <div class="code-block mb-6">
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createBy", String.class, getCurrentUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, getCurrentUsername());
    }

    private String getCurrentUsername() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getName();
        } catch (Exception e) {
            return "system";
        }
    }
}
            </div>

            <div class="bg-blue-50 p-6 rounded-lg mt-6">
                <h4 class="text-lg font-semibold text-blue-700 mb-3">
                    <i class="fas fa-check-circle mr-2"></i>配置需要自动填充的字段
                </h4>
                <div class="code-block bg-gray-900">
@Data
public class BaseEntity {
    
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    @TableField(fill = FieldFill.UPDATE)
    private Date updateTime;

    @TableField(fill = FieldFill.UPDATE)
    private String updateBy;
}
                </div>
            </div>
        </div>

        <!-- 方案三：Hibernate拦截器 -->
        <div class="comparison-card mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-shield-alt text-green-600 mr-3"></i>
                方案三：Hibernate Interceptor拦截器方案
            </h2>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-700">拦